package com.zackeus.mvvm.widget.zxing.QR;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.zackeus.common.utils.Utils;
import com.zackeus.mvvm.R;

/**
 * 二维码扫描器
 * @name: QrcodeActivity
 * @author: zz
 * @date: 2022/9/30
 */
public class QrcodeActivity extends Activity {

    //扩展闪光灯，边框，扫码线，去掉红线
    private DecoratedBarcodeView barcodeScannerView;
    private CaptureManager capture;

    private ImageView flashlight;
    private TextView flashlightText;
    private boolean isLightOn = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        barcodeScannerView = initializeContent();

        capture = new CaptureManager(this, barcodeScannerView);
        capture.initializeFromIntent(getIntent(), savedInstanceState);
        /* 关闭相机权限提示 回调中处理 */
        capture.setShowMissingCameraPermissionDialog(Boolean.FALSE);
        capture.decode();
    }

    protected DecoratedBarcodeView initializeContent() {
        setContentView(R.layout.activity_qrcode);

        barcodeScannerView = findViewById(R.id.barcode_scanner);
        LinearLayout flashlightView = findViewById(R.id.flashlightView);
        flashlight = findViewById(R.id.flashlight);
        flashlightText = findViewById(R.id.flashlightText);

        flashlight.setOnClickListener(v -> {
            if (isLightOn) {
                barcodeScannerView.setTorchOff();
                flashlight.setImageResource(R.drawable.light_close);
                flashlightText.setText("轻触点亮");
            } else {
                barcodeScannerView.setTorchOn();
                flashlight.setImageResource(R.drawable.light_open);
                flashlightText.setText("轻触关闭");
            }
            isLightOn = !isLightOn;
        });

        /* 如果没有闪光灯功能，就隐藏闪关灯功能 */
        if (!Utils.hasFlash()) {
            flashlightView.setVisibility(View.GONE);
        }

        return barcodeScannerView;
    }

    @Override
    protected void onResume() {
        super.onResume();
        capture.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        capture.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        capture.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        capture.onSaveInstanceState(outState);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }
}
